package com.changgou.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.changgou.canal.config.RabbitmqConfiguration;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;

@CanalEventListener
public class SpuListener {
    @Autowired
    private RabbitTemplate rabbitTemplate;
//监听数据库spu表
    @ListenPoint(schema = "changgou_goods",table = "tb_spu")
    public void goodsUp(CanalEntry.EntryType entryType,CanalEntry.RowData rowData){
        HashMap<String, Object> oldData = new HashMap<>();
        for (CanalEntry.Column column : rowData.getBeforeColumnsList()) {
            oldData.put(column.getName(),column.getValue());
        }
        HashMap<String, Object> newData = new HashMap<>();
        for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
            newData.put(column.getName(),column.getValue());
        }
        //商品上架
        if ("0".equals(oldData.get("is_marketable"))&&"1".equals(newData.get("is_marketable"))){
            rabbitTemplate.convertAndSend(RabbitmqConfiguration.GOODS_UP_EXCHANGE,"",oldData.get("id"));
        }
        //商品下架
        if ("1".equals(oldData.get("is_marketable"))&&"0".equals(newData.get("is_marketable"))){
            rabbitTemplate.convertAndSend(RabbitmqConfiguration.GOODS_DOWN_EXCHANGE,"",oldData.get("id"));
        }

        if ("0".equals(oldData.get("status"))&&"1".equals(newData.get("status"))){
            rabbitTemplate.convertAndSend(RabbitmqConfiguration.GOODS_UP_EXCHANGE,"",oldData.get("id"));
        }
    }
}
